package site.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import site.entity.User;
import site.service.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    @Qualifier("userService")
    @Autowired
    UserService userService;
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value="/", method=RequestMethod.GET)
	public String home() {
	    	logger.info("requesting home");
		return "login";
	}

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(Model model, @RequestParam("username") String username,
                                       @RequestParam("password") String password) {

        logger.info("Trying add user");

        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        user.setEnabled(true);
        user.setAccountNonExpired(true);
        user.setCredentialsNonExpired(true);
        user.setAccountNonLocked(true);

        userService.createUser(user);

        model.addAttribute("message", "successfully added");
        return "home";
    }
}

